(:
  A procedure to select the contents of an twitter csv file
  suppress duplicate ids, sort by time, add date, extract @to_user, remove html
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
<display>{
let $t := $arguments[1]
let $d := doc($t)/display/item
for $item at $p in $d
let $mess := string($item/info[@name="Message"])
let $date := 1000*xs:long($item/info[@name="time"]/time/date[1])
where if ($p=1) then fn:true() else fn:not($item/info[@name="MessageUrl"]/text()=$d[$p - 1]/info[@name="MessageUrl"]/text())
order by $date
return
  <item>{
    <info name="date">{ 
    	jj:formatDDMMYYYY(jj:unix-to-dateTime($date))
    }</info>,
    <info name="time">
    	<time><date>{$date}</date><duration>0</duration></time>
    </info>,
    $item/info[@name="User"],
    <info name="user_to">{
      if (fn:contains($mess, '@') and not(starts-with($mess, "RT"))) then 
      	if (fn:contains($mess, 'href=http')) then
      		fn:substring-before(fn:substring-after($mess,'twitter.com/'),'>') 
      	else
      		fn:substring-before(fn:substring-after($mess,'@'),' ') 
      else 
      	""
    }</info>,
    <info name="Message">{
      jj:remove-html(string($item/info[@name="Message"]))
    }</info>,
    $item/info[@name="MessageUrl"],
    $item/info[@name="src-anchor"]
  }</item>
}</display>
